<?php

/*
 * 用户权限控制
 */

namespace Model;

class User 
{
	public function login($username,$password)
	{
	    $result = array('valid'=>false,'errorCode'=>'00000','result'=>'');
	    
	    $userID = $this->getIdenty();

	    if($userID == true)
	    {
	        $code = '00001';
	        $result['errorCode'] = $code;
	        $result['result'] = $this->errorCode($code);
	        return $result;
	    }
	    
	    if(empty($username) == true)
	    {
	        $code = '00002';
	        $result['errorCode'] = $code;
	        $result['result'] = $this->errorCode($code);
	        $this->loginLog(0,$code);
	        return $result;
	    }
	    
	    if(empty($password) == true)
	    {
	        $code = '00003';
	        $result['errorCode'] = $code;
	        $result['result'] = $this->errorCode($code);
	        $this->loginLog(0,$code);
	        return $result;
	    }

		$user = MOA('permission_user')->where(array('userName'=>$username))->find();
		if(empty($user) == true)
		{
		    $code = '00004';
		    $result['errorCode'] = $code;
		    $result['result'] = $this->errorCode($code);
		    $this->loginLog(0,$code);
		    return $result;
		}
		
		$userID = $user['id'];
        
		$Crypt = new \Model\Crypt();
		$md5password = $Crypt->singlEncrypt($password,$user['publicKey']);
		if($md5password != $user['passWord'])
		{
		    $code = '00005';
		    $result['errorCode'] = $code;
		    $result['result'] = $this->errorCode($code);
		    $this->loginLog($userID,$code);
		    return $result;
		}
		
		if($user['status'] != 1)
		{
		    $code = '00006';
		    $result['errorCode'] = $code;
		    $result['result'] = $this->errorCode($code);
		    $this->loginLog($userID,$code);
		    return $result;
		}
		
		//success
		$this->setIdenty($userID);
		MOA('permission_user')->where(array('id'=>$userID))->save(array('lastLoginTime'=>time(),'lastLoginIp'=>ip2long(getIP())));
		
		$result['valid'] = true;
		$result['result'] = $this->errorCode($result['errorCode']);
		$this->loginLog($userID,$result['errorCode']);

		return $result;
	}
	
	public function loginLog($userID,$code)
	{
	    $data = array();
	    $data['userID'] = $userID;
	    $data['loginTime'] = time();
	    $data['loginIp'] = ip2long(getIP());
	    $data['code'] = $code;
	    
	    MOA('permission_user_login')->add($data);
	}
	
	public function register($data)
	{
		$return = array('valid'=>true,'msg'=>'');

		$user = MOA('permission_user')->where(array('userName'=>$data['username']))->find();
		
		if(empty($user) == true)
		{
			$Crypt = new \Model\Crypt();
			$md5password = $Crypt->singlEncrypt($data['password']);
			$insert = array();
			$insert['userName']      = $data['username'];
			$insert['passWord']      = $md5password['encrypt'];
			$insert['publicKey']     = $md5password['key'];
			$insert['createTime']    = time();
			$insert['createBy']      = $this->getIdenty();
			$insert['groupID']       = $data['groupID'];
			$insert['status']        = 1;
			$insert['position']      = $data['position'];
			$insert['mobile']        = $data['mobile'];
			$insert['role']          = $data['role'];

			$userID = MOA('permission_user')->add($insert);

			$rule = MOA('permission_rule')->where(array('groupID'=>$data['groupID']))->find();
			unset($rule['id']);
			$rule['groupID'] = 0;
			$rule['userID'] = $userID;
			
			MOA('permission_rule')->add($rule);
		}
		else
		{
			$return['valid'] = false;
			$return['msg'] = '账号已经存在';
		}
	
		return $return;
	}
	
	public function setIdenty($userID)
	{
	    session('DMS_TRIPVIP_ID',$userID);
	    cookie('DMS_TRIPVIP_ID',$userID,time()+24*3600*7);
	}
	
	public function getIdenty()
	{
	    if(session('DMS_TRIPVIP_ID') != null)
	    {
	        $userID = session('DMS_TRIPVIP_ID');
	    }
	    else if(cookie('DMS_TRIPVIP_ID') != null)
	    {
	        $userID = cookie('DMS_TRIPVIP_ID');
	    }
	    else
	    {
	        $userID = false;
	    }
	
	    return $userID;
	}
	
	public function logout()
	{
	    session('DMS_TRIPVIP_ID',null);
	    cookie('DMS_TRIPVIP_ID',null);
	}
	
	public function changePassword($id,$password)
	{
		$Crypt = new \Model\Crypt();
		$md5password = $Crypt->singlEncrypt($password);
		MOA('permission_user')->where(array('id'=>$id))->save(array('passWord'=>$md5password['encrypt'],'publicKey'=>$md5password['key']));
	}
	
	public function errorCode($code)
	{
	    $result = '未知错误';
	     
	    switch ($code)
	    {
	        case '00000':
	            $result = '登录成功';
	            break;
	        case '00001':
	            $result = '已登录状态';
	            break;
	        case '00002':
	            $result = '账号不能为空';
	            break;
	        case '00003':
	            $result = '密码不能为空';
	            break;
	        case '00004':
	            $result = '账号不存在';
	            break;
	        case '00005':
	            $result = '账号密码错误';
	            break;
	        case '00006':
	            $result = '账号已被锁定';
	            break;
	    }
	     
	    return $result;
	}
}
